﻿
//***********************************************************************
// * Module:   MsjHzGbgrddlhHandler
// * 作者:  XWG
// * Modified: 2013-05-29
// * Purpose: Definition of the Class MsjHzGbgrddlhHandler 业务逻辑处理Handler类
// ***********************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Data;
using XWG.Framework.Query;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using XWG.Framework.MSException;
using XWG.Framework.AuthUtility;
using XWG.Framework.AuthEntity;
using XWG.Framework.AuthSPConst;
using XWG.Framework.AuthBLL;
using XWG.JWBST.JWBSTEntity;
using XWG.JWBST.JWBSTSPConst;
using XWG.JWBST.JWBSTBLL;
using System.Collections;
using JWBSTAPP.CommonClass;
using XWG.Framework;

namespace JWBSTAPP.AjaxServer
{
    /// <summary>
    /// MsjHzGbgrddlhHandler 的摘要说明
    /// </summary>
    public class MsjHzGbgrddlhHandler : JWBSTAPP.CommonClass.HttpHandlerBase, System.Web.SessionState.IRequiresSessionState
    {

        #region 添加记录
        /// <summary>
        /// 添加记录
        /// </summary>
        protected void AddRecord()
        {
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //初始化返回类型
            try
            {
                MsjHzGbgrddlh mMsjHzGbgrddlh = new MsjHzGbgrddlh();
                mMsjHzGbgrddlh.Ddid = Guid.NewGuid().ToString(); //主键
                mMsjHzGbgrddlh.Sqrxm = context.Request.Form["texSqrxm"]; //申请人姓名
                mMsjHzGbgrddlh.Sqrxb = context.Request.Form["selSqrxb"]; //申请人性别(字典)
                mMsjHzGbgrddlh.Sqrcsrq = context.Request.Form["texSqrcsrq"]; //申请人出生日期
                mMsjHzGbgrddlh.Sqrmz = context.Request.Form["texSqrmz"]; //申请人民族(民族)
                mMsjHzGbgrddlh.Sqrsfzh = context.Request.Form["texSqrsfzh"]; //申请人身份证号
                mMsjHzGbgrddlh.Lxdh = context.Request.Form["texLxdh"]; //联系电话
                mMsjHzGbgrddlh.Yszdw = context.Request.Form["texYszdw"]; //原所在单位
                mMsjHzGbgrddlh.Qrdw = context.Request.Form["texQrdw"]; //迁入单位
                mMsjHzGbgrddlh.Hkszd = context.Request.Form["texHkszd"]; //户口所在地
                mMsjHzGbgrddlh.Sqqrd = context.Request.Form["texSqqrd"]; //申请迁入地
                mMsjHzGbgrddlh.Qrly = context.Request.Form["texQrly"]; //迁入理由
                mMsjHzGbgrddlh.Sljg = context.Request.Form["texSljg"]; //受理机关(字典)
                mMsjHzGbgrddlh.Cjsj = System.DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"); //创建时间(2013-05-28 09:13:01)//创建时间(2013-05-28 09:13:01)
                mMsjHzGbgrddlh.Cjr = Convert.ToString(context.Session["UserID"]); //创建人(人员ID)
                //mMsjHzGbgrddlh.Cjrdw = context.Request.Form["texCjrdw"]; //创建单位(单位ID)
                //mMsjHzGbgrddlh.Xgr = context.Request.Form["texXgr"]; //修改人(人员ID)
                //mMsjHzGbgrddlh.Xgrdw = context.Request.Form["texXgrdw"]; //修改人单位(单位ID)
                //mMsjHzGbgrddlh.Xgsj = context.Request.Form["texXgsj"]; //修改时间(2013-05-28 09:13:01)
                mMsjHzGbgrddlh.Enabled = Enabled.TRUE; //是否有效（1有效，0无效）
                mMsjHzGbgrddlh.Movesign = "0"; //是否传输（1已，0未）

                mResultModel = MsjHzGbgrddlhBLL.Insert(mMsjHzGbgrddlh, context.Session["UserID"], true); //新建操作
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, string.Empty);
                mResultModel.ErrorCode = -9999;
                mResultModel.ISSucceed = false;
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
        }
        #endregion

        #region 删除记录
        /// <summary>
        /// 删除记录
        /// </summary>
        protected void DeleteRecord()
        {
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //声明返回类型
            string paramKey = context.Request.Params["id"];
            try
            {
                if (string.IsNullOrEmpty(paramKey))
                {
                    mResultModel.ErrorCode = -9991;
                    mResultModel.ISSucceed = false;
                    context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回    
                    return;
                }
                //看随迁人员是否有关联数据有则提示“不可删除”
                QueryByPage Q = new QueryByPage();
                DataSet dt = Q.GetRecordByPage("*", "where enabled='1'", string.Empty, 1, 1, MsjHzGbgrddlhSqryConst.TableName, MsjHzGbgrddlhSqryConst.FieldKey, string.Empty);
                if (dt == null)
                {
                    MsjHzGbgrddlh mMsjHzGbgrddlh = new MsjHzGbgrddlh();
                    if (!string.IsNullOrEmpty(paramKey))
                    {
                        mResultModel = MsjHzGbgrddlhBLL.Delete(paramKey, context.Session["UserID"], true); //删除对应的信息
                    }
                }
                else
                {
                    mResultModel.ErrorCode = -999;
                }
                context.Response.Write(JsonConvert.SerializeObject(mResultModel));  //序列化后返回
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, string.Empty); //写入异常日志
                mResultModel.ErrorCode = -9999; //-9999代码是系统异常通用代码
                mResultModel.ISSucceed = false; //行为执行失败
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
        }
        #endregion

        #region 修改记录
        /// <summary>
        /// 修改记录
        /// </summary>
        protected void EditRecord()
        {
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //初始化返回类型
            MsjHzGbgrddlh mMsjHzGbgrddlh = new MsjHzGbgrddlh();

            string paramKey = context.Request.Params["id"];
            try
            {

                if (string.IsNullOrEmpty(paramKey))
                {
                    mResultModel.ErrorCode = -9991;
                    mResultModel.ISSucceed = false;
                    context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回    
                    return;
                }
                mMsjHzGbgrddlh = MsjHzGbgrddlhBLL.Load(paramKey); //根据键值获取实体信息
                if (mMsjHzGbgrddlh != null)
                {
                    mMsjHzGbgrddlh.Sqrxm = context.Request.Form["texSqrxm"]; //申请人姓名
                    mMsjHzGbgrddlh.Sqrxb = context.Request.Form["selSqrxb"]; //申请人性别(字典)
                    mMsjHzGbgrddlh.Sqrcsrq = context.Request.Form["texSqrcsrq"]; //申请人出生日期
                    mMsjHzGbgrddlh.Sqrmz = context.Request.Form["texSqrmz"]; //申请人民族(民族)
                    mMsjHzGbgrddlh.Sqrsfzh = context.Request.Form["texSqrsfzh"]; //申请人身份证号
                    mMsjHzGbgrddlh.Lxdh = context.Request.Form["texLxdh"]; //联系电话
                    mMsjHzGbgrddlh.Yszdw = context.Request.Form["texYszdw"]; //原所在单位
                    mMsjHzGbgrddlh.Qrdw = context.Request.Form["texQrdw"]; //迁入单位
                    mMsjHzGbgrddlh.Hkszd = context.Request.Form["texHkszd"]; //户口所在地
                    mMsjHzGbgrddlh.Sqqrd = context.Request.Form["texSqqrd"]; //申请迁入地
                    mMsjHzGbgrddlh.Qrly = context.Request.Form["texQrly"]; //迁入理由
                    mMsjHzGbgrddlh.Sljg = context.Request.Form["texSljg"]; //受理机关(字典)
                    //mMsjHzGbgrddlh.Cjsj = context.Request.Form["texCjsj"]; //创建时间(2013-05-28 09:13:01)
                    //mMsjHzGbgrddlh.Cjr = context.Request.Form["texCjr"]; //创建人(人员ID)
                    //mMsjHzGbgrddlh.Cjrdw = context.Request.Form["texCjrdw"]; //创建单位(单位ID)
                    mMsjHzGbgrddlh.Xgr = Convert.ToString(context.Session["UserID"]); //修改人(人员ID)
                    //mMsjHzGbgrddlh.Xgrdw = context.Request.Form["texXgrdw"]; //修改人单位(单位ID)
                    mMsjHzGbgrddlh.Xgsj = System.DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");  //修改时间(2013-05-28 09:13:01)
                    mMsjHzGbgrddlh.Enabled = context.Request.Form["selEnabled"]; //是否有效（1有效，0无效）
                    //mMsjHzGbgrddlh.Movesign = context.Request.Form["texMovesign"]; //是否传输（1已，0未）

                    mResultModel = MsjHzGbgrddlhBLL.Update(mMsjHzGbgrddlh, context.Session["UserID"], true); //新建操作
                }
                else
                {
                    mResultModel.ErrorCode = -9990; //没有获取所需的信息，信息可能已被删除
                    mResultModel.ISSucceed = false;
                }

                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, string.Empty);
                mResultModel.ErrorCode = -9999;
                mResultModel.ISSucceed = false;
                context.Response.Write(JsonConvert.SerializeObject(mResultModel)); //序列化后返回
            }
        }
        #endregion

        #region 获取实体记录
        /// <summary>
        /// 获取实体记录
        /// </summary>
        protected void GetModel()
        {
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            ResultModel mResultModel = new ResultModel(); //初始化返回类型
            string paramKey = context.Request.Params["id"];
            string json = string.Empty;
            try
            {
                MsjHzGbgrddlh mMsjHzGbgrddlh = new MsjHzGbgrddlh(); //声明实体
                if (!string.IsNullOrEmpty(paramKey))
                {
                    mMsjHzGbgrddlh = MsjHzGbgrddlhBLL.Load(paramKey); //根据键值获取管理员
                    if (mMsjHzGbgrddlh != null)
                    {
                        json = JsonConvert.SerializeObject(mMsjHzGbgrddlh);
                        Hashtable hashtable = new Hashtable();
                        if (!string.IsNullOrEmpty(mMsjHzGbgrddlh.Sqrxb))
                        {
                            hashtable.Add("SqrxbTxt", mMsjHzGbgrddlh.Sqrxb == "0" ? "男" : "女");
                        }

                        if (!string.IsNullOrEmpty(mMsjHzGbgrddlh.Enabled))
                        {
                            hashtable.Add("EnabledTxt", mMsjHzGbgrddlh.Enabled == "1" ? "有效" : "无效");
                        }
                        json = JsonString.AddAttribute(json, hashtable);
                    }
                }
            }
            catch (Exception ex)
            {
                MSEx.DealErr(ex, string.Empty);
                mResultModel.ErrorCode = -9990;
                mResultModel.ISSucceed = false;
                json = JsonConvert.SerializeObject(mResultModel);
            }

            context.Response.Write(json);
        }
        #endregion

        #region 获取数据
        /// <summary>
        /// 获取数据记录
        /// </summary>
        protected void QueryRecord()
        {
            TableMessage tm = new TableMessage();
            HearderMessage hm = new HearderMessage();
            List<HearderMessage> list = new List<HearderMessage>();
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";
            string pageSize = context.Request.Form["pageSize"];
            string pageIndex = context.Request.Form["pageIndex"];

            string xm = context.Request.Form["texSqrxm"]; //姓名
            string xb = context.Request.Form["selSqrxb"]; //性别(字典)
            string sfzh = context.Request.Form["texSqrsfzh"]; //身份证号
            string hkszd = context.Request.Form["texHkszd"]; //现户口所在地

            if (String.IsNullOrEmpty(pageSize) || String.IsNullOrEmpty(pageIndex) || !AnyString.IsNumeric(pageSize) || !AnyString.IsNumeric(pageIndex))
            {
                return;
            }

            DataTable dtMsjHzGbgrddlh = new DataTable();
            int intPageSize = Convert.ToInt32(pageSize);
            int intPageIndex = Convert.ToInt32(pageIndex);
            QueryByPage q = new QueryByPage();
            QueryInfo qInfo = new QueryInfo();

            SqlConditionList conditionList = new SqlConditionList();

            if (!String.IsNullOrEmpty(xm))
            {
                conditionList.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.LIKE, MsjHzGbgrddlhConst.Columns.Sqrxm, "%" + xm + "%"), SqlLogicOper.AND_LOGIC);
            }

            if (!String.IsNullOrEmpty(xb))
            {
                conditionList.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.EQUAL, MsjHzGbgrddlhConst.Columns.Sqrxb, xb), SqlLogicOper.AND_LOGIC);
            }

            if (!String.IsNullOrEmpty(sfzh))
            {
                conditionList.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.LIKE, MsjHzGbgrddlhConst.Columns.Sqrsfzh, "%" + sfzh + "%"), SqlLogicOper.AND_LOGIC);
            }

            if (!String.IsNullOrEmpty(hkszd))
            {
                conditionList.AddLogic(new SqlConditionList.SqlCondition(SqlCompareOper.LIKE, MsjHzGbgrddlhConst.Columns.Hkszd, "%" + hkszd + "%"), SqlLogicOper.AND_LOGIC);
            }

            string sqlWhere = " enabled='1' ";
            if (string.IsNullOrEmpty(conditionList.ToSql()))
            {
                sqlWhere = "where " + sqlWhere;
            }
            else
            {
                sqlWhere = conditionList.ToSql() + " and " + sqlWhere;
            }

            dtMsjHzGbgrddlh = q.GetRecordByPage("*", sqlWhere, string.Empty, intPageSize, intPageIndex, MsjHzGbgrddlhConst.TableName, MsjHzGbgrddlhConst.FieldKey, string.Empty).Tables[0];

            //翻译表DataTable中的特殊字段
            Hashtable columnData = new Hashtable();
            columnData.Add("0", "男");
            columnData.Add("1", "女");

            ResolveColumn resolveColumn = new ResolveColumn();
            resolveColumn.oldColumnName = "SQRXB";
            resolveColumn.newType = typeof(String);
            resolveColumn.columnData = columnData;

            ResolveColumn[] resolveColumnArray = new ResolveColumn[] { resolveColumn };
            dtMsjHzGbgrddlh = ResolveReference.ResolveDataTable(dtMsjHzGbgrddlh, resolveColumnArray);

            if (dtMsjHzGbgrddlh != null && dtMsjHzGbgrddlh.Rows.Count > 0)
            {
                for (int i = 0; i < dtMsjHzGbgrddlh.Columns.Count; i++)
                {
                    hm = new HearderMessage();
                    hm.name = dtMsjHzGbgrddlh.Columns[i].Caption;
                    list.Add(hm);
                }

                tm.Header = list;
                tm.data = dtMsjHzGbgrddlh;
                tm.totalCount = Convert.ToString((int)q.RowCount);
            }

            string json = JsonConvert.SerializeObject(tm);
            context.Response.Write(json);
        }

        protected void QueryGtRecord()
        {
            HttpContext context = HttpContext.Current;
            context.Response.ContentType = "text/plain";

            QueryInfo info = new QueryInfo();
            QueryByPage page = new QueryByPage();
            info.FieldKey = MsjHzGbgrddlhConst.FieldKey;
            info.PageIndex = 1;
            info.PageSize = 0;
            info.QTableName = MsjHzGbgrddlhConst.TableName;
            info.SelectFields = "*";

            info.Where = string.Empty;
            DataSet ds = page.GetRecordByPage(info);
            DataTable table = new DataTable();

            if (ds.Tables.Count > 0)
            {
                table = page.GetRecordByPage(info).Tables[0];
            }
            string json = JsonConvert.SerializeObject(table);
            context.Response.Write(json);
        }
        #endregion
    }
}


